package com.bird.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bird.bean.User;
import com.bird.dao.UserDao;

@Component(value = "userDao")
@Transactional
public class UserDaoImpl implements UserDao {

	private SessionFactory sessionfactory;
	
	public SessionFactory getSessionfactory() {
		return sessionfactory;
	}

	@Resource
	public void setSessionfactory(SessionFactory sessionfactory) {
		this.sessionfactory = sessionfactory;
	}
	
	public void saveUser(User user) {
//		 WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();  
//		System.out.println(wac.getBean("transactionManager"));  
		sessionfactory.getCurrentSession().save(user);
		
			throw new RuntimeException("12");
		
	}

	@SuppressWarnings("unchecked")
	@Transactional(readOnly=true)
	public List<User> findAllUsers() {
		String hql = "from User user order by user.id desc";
		Session session = sessionfactory.getCurrentSession();
		
		Query query = session.createQuery(hql);
		List<User> list = query.list();
		
		return list;
	}

}
